from django.shortcuts import render
from rest_framework import status

from course import cserializersa
from rest_framework.views import APIView
from rest_framework.response import Response
from home import serializers
from rest_framework.generics import ListAPIView
from rest_framework.pagination import PageNumberPagination
from django.core.cache import cache
from django.db.models import Q
# Create your views here.
from course import models
# 首页数据
class HomeView(APIView):
    def get(self,request):
        data=models.CateModle.objects.filter(recommend=True,parent=0).order_by("order")
        ser=cserializersa.SerSon(data,many=True)
        data=ser.data
        for item in data:
            item["son"]=item["son"][0:3]
        return Response({"mes":"ok","data":ser.data})


# 获取当前分类下子分类推荐在首页的课程
class CateCourse(APIView):
    def get(self,request):
        parentid=request.query_params.get("pid")
        data=models.CateModle.objects.filter(id=parentid).first()
        ser=cserializersa.SerSon(data)
        sonlist=[]
        for item in ser.data["son"]:
            sonlist.append(item["id"])
        print(sonlist)
        homecourse=models.Course.objects.filter(category__in=sonlist).order_by("order")[:4]
        print(homecourse)
        coursedata=cserializersa.Courseser(homecourse,many=True).data
        datalist=ser.data
        datalist["course"]=coursedata
        return Response({"mes":"ok","data":datalist})


class NavSerializer(APIView):
    def get(self,request):
        navkey="navlist"
        catchdata=cache.get(navkey)
        if catchdata:
            return Response({"msg":"ok","data":catchdata})
        else:
            pos=request.query.get("pos")
            navlist=models.CateModle.objects.filter(position=pos,isshow=True).order_by("order")
            ser=serializers.Sernav(navlist,many=True)
            cache.set(navkey,ser.data,60*60)
            return Response({"msg":"ok","data":ser.data})

class GetParentCate(APIView):
    def get(self,request):
        parentcate=models.CateModle.objects.filter(parent_id=0).order_by("order")
        ser=cserializersa.SerCourse(parentcate,many=True)
        return Response({"mes":"ok","data":ser.data})

class GetSonCate(APIView):
    def get(self,request):
        parentid=request.query_params.get("pid")
        if parentid:
            soncate=models.CateModle.objects.filter(parent=parentid)
        else:
            soncate=models.CateModle.objects.filter(parent_id=0)
        ser=cserializersa.SerCourse(soncate,many=True)
        return Response({"mes":"ok","data":ser.data})
class CouresPage(PageNumberPagination):
    page_size=2
    page_size_query_param="size"
    page_query_param="page"

class GetCourse(ListAPIView):
    serializer_class=cserializersa.Courseser
    pagination_class=CouresPage
    def get_queryset(self):
        query=Q(isshow=True)
        cateid=self.request.query_params.get("cateid")
        directionid=self.request.query_params.get("directionid")
        level=self.request.query_params.get("level")
        orderdata=self.request.query_params.get("ordering")
        if level:
            query=query&Q(level=level)
        if cateid:
            query=query&Q(category=cateid)
        else:
            if directionid:
                son=models.CateModle.objects.filter(parent=directionid)
                sondata=[]
                for i in son:
                    sondata.append(i.id)
                query=query&Q(category__in=sondata)
        if orderdata:
            queryset = models.Course.objects.filter(query).order_by("order",orderdata)
        else:
            queryset = models.Course.objects.filter(query).order_by("order")
        return queryset

class GetCourseDetail(APIView):
    def get(self,request):
        id=request.query_params.get("id")
        data=models.Course.objects.filter(id=id).first()
        ser=cserializersa.CourseSerializer(data)
        return Response({"msg":"ok","data":ser.data})





